import { request } from "../../request/index.js";
import regeneratorRuntime from '../../lib/runtime/runtime';
Page({

    /**
     * 页面的初始数据
     */
    data: {
        goodsObj: {},
        //商品是否被收藏
        isCollect: false
    },
    //商品对象
    GoodsInfo: {},

    /**
     * 生命周期函数--监听页面加载
     */
    onShow: function() {
        let pages = getCurrentPages();
        let currentPages = pages[pages.length - 1];
        let options = currentPages.options;
        const { goods_id } = options;
        this.getGoodsDetail(goods_id);

    },


    //获取商品详情数据
    async getGoodsDetail(goods_id) {
        const goodsObj = await request({ url: "/goods/detail", data: { goods_id } });
        this.GoodsInfo = goodsObj;
        //获取缓存中视频收藏
        let collect = wx.getStorageSync("collect") || [];
        //判断当前商品是否被收藏
        let isCollect = collect.some(v => v.goods_id === this.GoodsInfo.goods_id);
        this.setData({
            goodsObj: {
                goods_name: goodsObj.goods_name,
                goods_price: goodsObj.goods_price,
                //iphone不识别webp
                //临时修改


                goods_introduce: goodsObj.goods_introduce.replace(/\.webp/g, '.jpg'),
                pics: goodsObj.pics
            },
            isCollect
        })


    },


    //点击轮播图 放大预览
    handlePreviewImage(e) {
        //构造要预览的图片数组
        const urls = this.GoodsInfo.pics.map(v => v.pics_mid)
            //接受传递过来的url
        const current = e.currentTarget.dataset.url;

        wx.previewImage({
            current, // 当前显示图片的http链接
            urls, // 需要预览的图片http链接列表
        });
    },

    //点击加入购物车
    handleCartAdd() {
        //获取缓存中购物车数组
        let cart = wx.getStorageSync("cart") || [];
        //判断商品对象是否存在购物车数组
        let index = cart.findIndex(v => v.goods_id === this.GoodsInfo.goods_id);
        if (index === -1) {
            //不存在 第一次添加
            this.GoodsInfo.num = 1;
            this.GoodsInfo.checked = true;
            cart.push(this.GoodsInfo);
        } else {
            //存在购物车数据 num++
            cart[index].num++;
        }
        //把购物车重新添加到缓存
        wx.setStorageSync("cart", cart);
        //弹窗
        wx.showToast({
            title: '加入成功',
            icon: 'success',
            mask: true
        })

    },

    handleCollect() {
        let isCollect = false;
        //获取缓存在的商品数组
        let collect = wx.getStorageSync("collect") || [];
        //判断该商品是否被收藏过
        let index = collect.findIndex(v => v.goods_id === this.GoodsInfo.goods_id);
        //当index=！-1表示收藏过
        if (index !== -1) {
            //能找到 已经收藏过 在数组中删除该商品
            collect.splice(index, 1);
            isCollect = false;
            wx.showToast({
                title: '取消成功',
                icon: 'success',
                mask: 'true'
            });
        } else {
            //没有收藏过
            collect.push(this.GoodsInfo);
            isCollect = true;
            wx.showToast({
                title: '收藏成功',
                icon: 'success',
                mask: 'true'
            });
        }
        //把数组存入缓存中
        wx.setStorageSync("collect", collect);
        //修改data中的属性
        this.setData({
            isCollect
        });

    }


})